import * as _ from 'lodash'

const required = (rule, value, callback) => {
  if (_.isEmpty(value)) {
    callback(new Error(rule.message))
  } else {
    callback()
  }
}

const isNum = (rule, value, callback) => {
  if (/^(-?\d+)(\.\d+)?$/.test(value) || _.isEmpty(value)) {
    callback()
  } else {
    callback(new Error(rule.message))
  }
}

/** 验证整数 **/
const isInteger = (rule, value, callback) => {
  if (/^(0|-?[1-9][0-9]*)$/.test(value) || _.isEmpty(value)) {
    callback()
  } else {
    callback(new Error(rule.message))
  }
}

/** 验证 数字  小数位最多2位 **/
const isDouble = (rule, value, callback) => {
  if (/^\d+(\.\d{1,2})?$/.test(value) || _.isEmpty(value)) {
    callback()
  } else {
    callback(new Error(rule.message))
  }
}

const rangeV = (rule, value, callback) => {
  if (rule.minV === null || rule.minV === undefined) {
    rule.minV = -Infinity
  }
  if (rule.maxV === null || rule.maxV === undefined) {
    rule.maxV = Infinity
  }
  if (_.isEmpty(value) || (value >= rule.minV && value <= rule.maxV)) {
    callback()
  } else {
    callback(new Error(rule.message))
  }
}

const rules = {
  times: [
    {required: true, validator: required, message: '请输入时间段', trigger: 'blur'}
  ],
  beginTime: [
    {required: true, validator: required, message: '请输入开始时间', trigger: 'blur,change'}
  ],
  endTime: [
    {required: true, validator: required, message: '请输入结束时间', trigger: 'blur,change'}
  ],
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'}
  ],
  orgName: [
    {required: true, message: '请输入组织名', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'}
  ],
  newPassword: [
    {required: true, message: '请输入新密码', trigger: 'blur'}
  ],
  conPassword: [
    {required: true, message: '请再次输入新密码', trigger: 'blur'}
  ],
  joinDate: [
    {required: true, validator: required, message: '请输入入职日期', trigger: 'blur,change'}
  ],
  name: [
    {required: true, message: '请输入姓名', trigger: 'blur'}
  ],
  mobileR: [
    {required: true, message: '请输入手机号', trigger: 'blur'}
  ],
  mobile: [
    {min: 11, max: 11, message: '请输入正确的手机号码', trigger: 'blur,change'}
  ],
  emailR: [
    {required: true, message: '请输入邮箱地址', trigger: 'blur'}
  ],
  email: [
    {type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change'}
  ],
  roleOption: [
    {required: true, validator: required, message: '请选择角色', trigger: 'blur,change'}
  ],
  department: [
    {required: true, validator: required, message: '请选择部门', trigger: 'blur,change'} // 这个list得自己校验才行
  ],
  organization: [
    {required: true, validator: required, message: '请选择组织', trigger: 'blur,change'} // 这个list得自己校验才行
  ],
  vacationType: [
    {required: true, validator: required, message: '请选择请假类别', trigger: 'blur,change'} // 这个list得自己校验才行
  ],
  sealName: [
    {required: true, validator: required, message: '请选择印章名称', trigger: 'blur,change'} // 这个list得自己校验才行
  ],
  hours: [
    {validator: isNum, message: '请输入正确的数字', trigger: 'blur,change'},
    {required: true, validator: required, message: '请填写时长', trigger: 'blur,change'}
  ],
  range: function ({min, max}, msg, trigger = 'blur,change') {
    return {validator: rangeV, minV: min, maxV: max, message: msg, trigger: trigger}
  },
  positionLevel: [
    {required: true, message: '请选择职务', trigger: 'blur,change'}
  ],
  roleNameR: [
    {required: true, message: '请输入角色名称', trigger: 'blur'}
  ],
  dept: {
    deptName: [
      {required: true, message: '请输入部门名称', trigger: 'blur'}
    ]
  },
  reasonType: [
    {required: true, validator: required, message: '请选择加班原因', trigger: 'blur,change'}
  ],
  resource: {
    resName: [
      {required: true, message: '请输入菜单名称', trigger: 'blur'}
    ],
    code: [
      {required: true, message: '请输入code', trigger: 'blur'}
    ],
    url: [
      {required: true, message: '请输入url', trigger: 'blur'}
    ],
    parentId: [
      {required: true, message: '请选择父菜单', trigger: 'blur,change'}
    ],
    type: [
      {required: true, message: '请选择功能类型', trigger: 'blur,change', type: 'number'}
    ]
  },
  dict: {
    name: [
      {required: true, message: '请输入名称', trigger: 'blur'}
    ],
    type: [
      {required: true, message: '请输入code', trigger: 'blur'}
    ],
    valid: [
      {required: true, message: '请选择状态', trigger: 'blur', type: 'number'}
    ]
  },
  dictData: {
    cname: [
      {required: true, message: '请输入中文名称', trigger: 'blur'}
    ],
    ename: [
      {required: true, message: '请输入英文名称', trigger: 'blur'}
    ],
    code: [
      {required: true, message: '请输入code', trigger: 'blur'}
    ],
    sortNo: [
      {required: true, message: '请输入排序号', trigger: 'blur'}
    ],
    valid: [
      {required: true, message: '请选择状态', trigger: 'blur', type: 'number'}
    ]
  },
  attendance: {
    userId: [
      {required: true, validator: required, message: '请输入姓名', trigger: 'blur,change'}
    ],
    remark: [
      {required: true, message: '请填写说明', trigger: 'blur'}
    ],
    times: [
      {required: true, validator: required, message: '请输入补打卡时间', trigger: 'blur,change'}
    ]
  },
  from: [
    {required: true, validator: required, message: '请选择出发地', trigger: 'blur,change'}
  ],
  to: [
    {required: true, validator: required, message: '请选择目的地', trigger: 'blur,change'}
  ],
  apply: {
    starting: [
      {required: true, message: '请输入出发地', trigger: 'blur,change'}
    ],
    arriving: [
      {required: true, message: '请输入目的地', trigger: 'blur,change'}
    ]
  },
  reason: [
    {required: true, validator: required, message: '请填写说明', trigger: 'blur,change'}
  ],
  startKilometer: [
    {validator: isNum, message: '请输入正确的数字', trigger: 'blur,change'},
    {required: true, validator: required, message: '请输入起始公里数', trigger: 'blur,change'}
  ],
  endKilometer: [
    {validator: isNum, message: '请输入正确的数字', trigger: 'blur,change'},
    {required: true, validator: required, message: '请输入结束公里数', trigger: 'blur,change'}
  ],
  businessCars: {
    userId: [
      {required: true, validator: required, message: '请选择用车人', trigger: 'blur,change'}
    ],
    reason: [
      {required: true, validator: required, message: '请输入用车事由及地点', trigger: 'blur,change'}
    ],
    startKilometer: [
      {validator: isNum, message: '请输入正确的数字', trigger: 'blur,change'}
    ],
    driver: [
      {required: true, validator: required, message: '请填写出车人', trigger: 'blur,change'}
    ]
  },
  updateHours: [
    {required: true, message: '请填写需补录时长', trigger: 'blur,change'},
    {validator: isDouble, message: '请填写正确的数字', trigger: 'blur,change'}
  ],
  overTimeCode: [
    {required: true, validator: required, message: '请选择加班单据', trigger: 'blur,change'}
  ],
  appraisal: {
    workContent: [
      {required: true, message: '请填写工作任务内容', trigger: 'blur,change'}
    ],
    weight: [
      {validator: isInteger, message: '请输入整数', trigger: 'blur,change'},
      {validator: rangeV, minV: 1, message: '请输入大于0的整数', trigger: 'blur,change'},
      {required: true, validator: required, message: '请填写权重', trigger: 'blur,change'}
    ],
    selfRated: [
      {validator: isInteger, message: '请输入整数', trigger: 'blur,change'},
      {validator: rangeV, minV: 1, message: '请输入大于0的整数', trigger: 'blur,change'},
      {required: true, validator: required, message: '请填写自评分', trigger: 'blur,change'}
    ],
    score: [
      {validator: isInteger, message: '请输入整数', trigger: 'blur,change'},
      {validator: rangeV, minV: 0, message: '请输入大于等于0的整数', trigger: 'blur,change'}
    ],
    appraisalDate: [
      {required: true, validator: required, message: '请填写考核期', trigger: 'blur,change'}
    ]
  },
  resourceUse: {
    name: [
      {required: true, validator: required, message: '请填写资源名称', trigger: 'blur,change'}
    ],
    type: [
      {required: true, validator: required, message: '请选择资源类型', trigger: 'blur,change'}
    ]
  },
  archivingDate: [
    {required: true, validator: required, message: '请填写存档日期', trigger: 'blur,change'}
  ],
  plateNo: [
    {required: true, validator: required, message: '请选择车牌号', trigger: 'blur,change'} // 这个list得自己校验才行
  ],
  taskName: [
    {required: true, message: '请输入任务名称', trigger: 'blur,change'}
  ],
  description: [
    {required: true, message: '请输入描述', trigger: 'blur,change'}
  ],
  dateRange: [
    {type: 'array', required: true, message: '请选择日期范围', trigger: 'change'}
  ],
  cronExpression: [
    {required: true, message: '请输入cron表达式', trigger: 'blur,change'}
  ],
  serviceName: [
    {required: true, message: '请输入服务名', trigger: 'blur,change'}
  ],
  method: [
    {required: true, message: '请输入方法名', trigger: 'blur,change'}
  ],
  notEmpty: [
    {required: true, validator: required, message: '不能为空', trigger: ['blur', 'change']}
  ],
  numeric: [
    {validator: isNum, message: '只能为数字', trigger: 'blur,change'}
  ],
  integer: [
    {validator: isInteger, message: '请输入整数', trigger: 'blur,change'},
    {validator: rangeV, minV: 1, message: '请输入大于0的整数', trigger: 'blur,change'},
    {validator: rangeV, maxV: 100000000, message: '请输入小于100000000的整数', trigger: 'blur,change'}
  ],
  airIssueRule: {
    airline: [{required: true, validator: required, message: '航空公司不能为空', trigger: ['blur', 'change']}],
    issueChanel: [{required: true, validator: required, message: '改选项不能为空', trigger: ['blur', 'change']}],
    applyPolicyCodes: [{required: true, validator: required, message: '适用政策代码不能为空', trigger: ['blur', 'change']}]
  },
  b2b: {
    b2bUsername: [{required: true, validator: required, message: 'b2b账户不能为空', trigger: ['blur', 'change']}],
    b2bPwd: [{required: true, validator: required, message: 'b2b登录密码不能为空', trigger: ['blur', 'change']}],
    payAccountId: [{required: true, validator: required, message: '支付账号不能为空', trigger: ['blur', 'change']}]
  },
  orderIssueDetail: {
    issueChannel: [{required: true, validator: required, message: '出票渠道不能为空', trigger: ['blur', 'change']}],
    officeNo: [{required: true, validator: required, message: 'OFFICE号不能为空', trigger: ['blur', 'change']}],
    payWay: [{required: true, validator: required, message: '支付方式不能为空', trigger: ['blur', 'change']}],
    birthDate: [{required: true, validator: required, message: '生日不能为空', trigger: ['blur', 'change']}],
    ticketNo: [{required: true, validator: required, message: '票号不能为空', trigger: ['blur', 'change']}]
  },
  orderSupplementDetail: {
    ageType: [{required: true, validator: required, message: '不能为空', trigger: ['blur', 'change']}],
    passengerName: [{required: true, validator: required, message: '姓名不能为空', trigger: ['blur', 'change']}],
    cardNo: [{required: true, validator: required, message: '证件号码不能为空', trigger: ['blur', 'change']}]
  },
  orderRemark: {
    content: [{required: true, validator: required, message: '备注不能为空', trigger: ['blur', 'change']}]
  },
  orderRefundDetail: {
    shopCode: [{required: true, validator: required, message: '所属网店不能为空', trigger: ['blur', 'change']}],
    refundType: [{required: true, validator: required, message: '退票类型不能为空', trigger: ['blur', 'change']}],
    payFlightAgency: [{required: true, validator: required, message: '不能为空', trigger: ['blur', 'change']}],
    airlineRefund: [{required: true, validator: required, message: '不能为空', trigger: ['blur', 'change']}],
    issueChannel: [{required: true, validator: required, message: '出票渠道不能为空', trigger: ['blur', 'change']}],
    pnr: [{required: true, validator: required, message: '不能为空', trigger: ['blur', 'change']}],
    latestCancelPnrTime: [{required: true, validator: required, message: '执行XEPNR时间不能为空', trigger: ['blur', 'change']}],
    pnrCode: [{required: true, validator: required, message: '不能为空', trigger: ['blur', 'change']}],
    pnrText: [{required: true, validator: required, message: '不能为空', trigger: ['blur', 'change']}]
  },
  orderSupplement: {
    pnrCode: [{required: true, validator: required, message: '不能为空', trigger: ['blur', 'change']}],
    pnrText: [{required: true, validator: required, message: '不能为空', trigger: ['blur', 'change']}],
    localOrderId: [{required: true, validator: required, message: '不能为空', trigger: ['blur', 'change']}]
  },
  policy: {
    airline: [{required: true, validator: required, message: '航空公司不能为空', trigger: ['blur', 'change']}],
    departDateStart: [{required: true, validator: required, message: '不能为空', trigger: ['blur', 'change']}],
    departDateEnd: [{required: true, validator: required, message: '不能为空', trigger: ['blur', 'change']}]
  },
  customBookingRule: {
    hoursBefore: [{required: true, validator: required, message: '不能为空', trigger: ['blur', 'change']}],
    rate: [{required: true, validator: required, message: '不能为空', trigger: ['blur', 'change']}]
  }
}

function install (Clazz) {
  Clazz.prototype.$checkRules = rules
}

export default {
  install
}
